<template>
	<view class="address">
		<view class="list" v-if="list.length">
			<view class="addOne" v-for="(item,index) in list" :key='index' @tap.stop='selAdd(item)'>
				<view class="addInfo">
					<view class="userInfo dFlex jStart_aCenter">
						<view class="user">{{item.receiver}}</view>
						<view class="tel">{{item.phone}}</view>
					</view>
					<view class="add">{{item.address + item.address_detail}}</view>
				</view>
				<view class="tools dFlex jBetween_aCenter">
					<view class="status dFlex jStart_aCenter" @tap.stop='setDefault(item)'>
						<view class="statusIcon imgPublic">
							<image v-if="item.is_default" src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/sel.png" mode="widthFix"></image>
							<image v-else src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/unsel.png" mode="widthFix"></image>
						</view>
						<text>默认地址</text>
					</view>
					<view class="btns dFlex jEnd_aCenter">
						<view class="btnOne dFlex jEnd_aCenter" @tap.stop='opAdd(item)'>
							<!-- <view class="btnIcon imgPublic">
								<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/editIcon.png" mode="widthFix"></image>
							</view> -->
							<text>编辑</text>
						</view>
						<view class="btnOne dFlex jEnd_aCenter" @tap.stop='del(item.id)'>
							<!-- <view class="btnIcon imgPublic">
								<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/delIcon.png" mode="widthFix"></image>
							</view> -->
							<text>删除</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="list empty imgPublic" v-else>
			<image src="https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/empty.png" mode="widthFix"></image>
		</view>
		<view class="addBtn" @tap='opAdd()'>添加收货地址</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[],
			};
		},
		onShow(){
			this.initList();
		},
		onShareAppMessage() {
			this.tool.monitor();
			return {
				title:'加一精选',
				path:'/pages/seller',
				imageUrl:'https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/logo.png',
				desc:'这里有众多优质好物等您来选择',
			} 
		},
		onShareTimeline() {
			this.tool.monitor();
			let share = {
				title:'加一精选',
				path:'/pages/seller',
				imageUrl:'https://zl-tuangou.oss-cn-qingdao.aliyuncs.com/cMini/logo.png',
				desc:'这里有众多优质好物等您来选择',
			}
			return share;
		},
		methods:{
			// 地址列表
			initList(){
				this.tool.getData('/api/user/getAddressList',{}).then(res=>{
					if(res){
						this.list = res;
					}
				}).catch(err=>{
					console.log(err);
				})
			},
			// 选择该地址
			selAdd(line){
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - 2];// 上级页面实例
				prevPage.$vm.addInfo = line;
				uni.navigateBack({
					delta:1,
				})
			},
			// 新增或编辑地址
			opAdd(line){
				if(line){
					uni.navigateTo({
						url:'./opAdd?line='+JSON.stringify(line),
						fail:(err)=>{
							console.log(err);
						}
					}) 
				}else{
					uni.navigateTo({
						url:'./opAdd',
					})
				}
			},
			// 删除地址
			del(id){
				uni.showModal({
					content:'确认删除该地址？',
					success: (op) => {
						if(op.confirm){
							this.tool.getData('/api/user/deleteAddress',{
								id:id
							}).then(res=>{
								if(res){
									this.initList();
								}
							}).catch(err=>{
								console.log(err);
							})
						}
					}
				})
			},
			// 设置默认地址
			setDefault(line){
				if(!line.is_default){
					this.tool.getData('/api/user/setDefaultAddress',{
						id:line.id
					}).then(res=>{
						if(res){
							uni.showToast({
								title:'已修改默认地址',
								icon:"success"
							})
							this.initList();
						}
					}).catch(err=>{
						console.log(err);
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background:#F6F6F6;
	}
	.address{
		width: 100%;
		padding: 12px;
		box-sizing: border-box;
		.addBtn{
			position: fixed;
			bottom: 32px;
			text-align: center;
			color: white;
			border-radius: 40px;
			font-size: 16px;
			width: 74.4%;
			left: 12.8%;
			padding: 22rpx 0;
			background: linear-gradient(180deg, #FC9700 0%, #FF5000 100%);
		}
		.list{
			.addOne{
				width: 100%;
				background: white;
				border-bottom: 24px;
				border-radius: 8px;
				margin-bottom: 12px;
				.tools{
					padding: 12px;
					box-sizing: border-box;
					color: #666666;
					.btns{
						width: auto;
						.btnOne{
							padding: 6rpx 20rpx;
							border-radius: 8rpx;
							border: 1px solid #9A9A9A;
							margin-left: 16px;
							.btnIcon{
								width: 17px;
								margin-right: 4px;
							}
						}
					}
					.status{
						width: auto;
						.statusIcon{
							width: 16px;
							height: 16px;
							margin-right: 5px;
						}
					}
				}
				.addInfo{
					padding: 12px;
					box-sizing: border-box;
					border-bottom: 1px solid #F5F5F5;
					.userInfo{
						font-weight: bold;
						margin-bottom: 8px;
						color: #333333;
						font-size: 15px;
						.user{
							margin-right: 8px;
						}
					}
					.add{
						color: #666666;
					}
				}
			}
			.addOne:last-child{
				margin: 0;
			}
		}
	}
</style>
